package com.iamteer.day18.dao;

import java.util.ArrayList;
import java.util.Iterator;

import com.iamteer.day18.model.User;

public class UserDao {
	public static final int INVALID_USER = -1;
	public static final int NORMAL_USER = 0;
	public static final int ADMIN_USER = 1;
	
	private static ArrayList<User> users = new ArrayList<User>();
	private static User currentUser = null;
	private static int currentIndex = -1;
	
	
	public boolean signUp(User user) {
		boolean signUpSuccessful = false;
/*		Scanner scanner = new Scanner(System.in);
		String username;
		String password;
		while (true) {
			System.out.println("请输入注册账户：(6～18 位字母或数字，首位必须为字母)");
			username = scanner.next();
			if (username.matches("[a-zA-Z][a-zA-Z0-9]{5,17}")) {
				break;
			} else {
				System.out.println("账户不符合规则：6～18 位字母或数字，首位必须为字母");
			}
		}
		while (true) {
			System.out.println("请输入注册密码：(6～18 位字母或数字)");
			password = scanner.next();
			if (password.matches("[a-zA-Z0-9]{6,18}")) {
				break;
			} else {
				System.out.println("密码不符合规则：6～18 位字母或数字");
			}
		}
		users.add(new User(username, password));*/
		
/*		if (user.getUsername().matches("[a-zA-Z][a-zA-Z0-9]{5,17}")) {
			if (user.getPassword().matches("[a-zA-Z0-9]{6,18}")) {
				System.out.println("当前用户有：" + users);
				signUpSuccessful = true;
			} else {
				System.out.println("密码不符合规则：6～18 位字母或数字");
			}
		} else {
			System.out.println("账户不符合规则：6～18 位字母或数字，首位必须为字母");
		}
		return signUpSuccessful;*/
		signUpSuccessful = users.add(user);
		System.out.println("当前用户有：" + users);
		return signUpSuccessful;
	}
	
	public int signIn(User loginUser) {
		int userCode = INVALID_USER;
//		Iterator<User> iterator = users.iterator();
//		while (iterator.hasNext()) {
//			User user = (User) iterator.next();
//			if (user.equals(loginUser)) {
//				signInSuccessful = true;
//				break;
//			}
//		}
//		return signInSuccessful;
		if (users.contains(loginUser)) {
			if (loginUser.getUsername().equals("admin")) {
				userCode = ADMIN_USER;
			}else {
				userCode = NORMAL_USER;
			}
			currentUser = loginUser;
			currentIndex = users.indexOf(loginUser);
		} else {
			currentUser = null;
			currentIndex = -1;
			userCode = INVALID_USER;
		}
		
		return userCode;
	}
	
	public boolean changePassword(String newPassword) {
		boolean successful = false;
//
//		int index = 0;
//		Iterator<User> iterator = users.iterator();
//		while (iterator.hasNext()) {
//			User user = (User) iterator.next();
//			if (user.equals(user2Change)) {
//				user.setPassword(newPassword);
//				users.set(index, user);
//				signInSuccessful = true;
//				break;
//			}
//			index++;
//		}
//		return signInSuccessful;
		currentUser.setPassword(newPassword);
		users.set(currentIndex, currentUser);
		System.out.println("密码修改成功：" + currentUser);
		return successful;
	}
	
	public boolean changeUsername(String newUsername) {
		boolean signInSuccessful = false;

		User newUser = new User(newUsername, currentUser.getPassword());
		if (users.contains(newUser)) {
			System.out.println("账户已存在，修改失败！");
		} else {
			currentUser.setUsername(newUsername);
			users.set(currentIndex, currentUser);
			System.out.println("账户修改成功：" + currentUser);
			signInSuccessful = true;
		}
		return signInSuccessful;
	}
	
	public boolean deleteUser(String userName) {
		if (userName.equals("admin")) {
			System.out.println("不能删除管理员账户！");
			return false;
		}
		if (currentUser.getUsername().equals("admin")) {
			Iterator<User> iterator = users.iterator();
			while (iterator.hasNext()) {
				User user = (User) iterator.next();
				if (user.getUsername().equals(userName)) {
					users.remove(user);
					System.out.println("账户已删除！");
					return true;
				}
			}
			System.out.println("账户不存在！");
		}
		return false;
	}
	
	public ArrayList<User> listUsers() {
		return users;
	}
}
